\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{exercices}[02/03/2014]

\newif\ifex@print\ex@printtrue
\newif\ifs@lprint\s@lprinttrue
\DeclareOption{exercices}{\ex@printtrue\s@lprintfalse}
\DeclareOption{solutions}{\s@lprinttrue\ex@printfalse}
\DeclareOption*{\PackageWarning{exercices}{Unknown option `\CurrentOption'}}
\ProcessOptions\relax

\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}
\RequirePackage[frenchb]{babel}
\RequirePackage{textcomp}
\RequirePackage[autolanguage]{numprint}
\RequirePackage{tikz}
\RequirePackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\RequirePackage{geometry}
\RequirePackage{booktabs,multirow,multicol}
\RequirePackage{graphicx}
\RequirePackage{paralist}
%\RequirePackage{comment}
\frenchbsetup{ThinSpaceInFrenchNumbers,GlobalLayoutFrench}

%%%%%%%%%%%%%%%%
%% for numbering and toc
%%%%%%%%%%%%%%%%
\newcommand*\l@exo[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
     \addvspace{10pt \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      Exercice~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\newcommand*\l@solu[2]{%
  \ifnum \c@tocdepth >\z@
    \addpenalty\@secpenalty
     \addvspace{10pt \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      Solution~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\newcommand*\l@answer[2]{%
  \ifnum \c@tocdepth >\@ne
    \addpenalty\@secpenalty
     \addvspace{10pt \@plus\p@}%
    \setlength\@tempdima{1.0em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \normalfont
      \advance\leftskip\@tempdima
      \hskip \leftskip
      R\'eponse~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\newcommand*\l@answeri[2]{%
  \ifnum \c@tocdepth > \tw@
    \addpenalty\@secpenalty
     \addvspace{5pt \@plus\p@}%
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \small
      \advance\leftskip\@tempdima
      \hskip \leftskip
      R\'eponse~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}
\newcommand*\l@answerii[2]{%
  \ifnum \c@tocdepth >\three@
    \addpenalty\@secpenalty
     \addvspace{3pt \@plus\p@}%
    \setlength\@tempdima{2em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \small
      \advance\leftskip\@tempdima
      \hskip \leftskip
      R\'eponse~#1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
    \endgroup
  \fi}

\def\exomark#1{%
  \markright {Exercice\ \MakeUppercase{%
    \ifnum \c@secnumdepth >\m@ne
      \theexo\quad
    \fi
  #1}}}


\def\solumark#1{%
  \markright {\MakeUppercase{%
    \ifnum \c@secnumdepth >\m@ne
      \thesolu\quad
    \fi
  #1}}}

\def\answermark#1{%
  \markright {\MakeUppercase{%
    \ifnum \c@secnumdepth >\m@ne
      \theanswer\quad
    \fi
  #1}}}

\def\answerimark#1{%
  \markright {\MakeUppercase{%
    \ifnum \c@secnumdepth >\m@ne
      \theansweri\quad
    \fi
  #1}}}

\def\answeriimark#1{%
  \markright {\MakeUppercase{%
    \ifnum \c@secnumdepth >\m@ne
      \theanswerii\quad
    \fi
  #1}}}

\newif\ifgobble@\gobble@false
\newcommand{\gobble@one}[1]{\def\next\relax\ifgobble@\relax\def\next\gobble@one\else#1\fi\next}%
\newcommand{\gobble@all}[1]{\gobble@one{#1}}
\newcommand{\gobble@all@end}{}
\newcommand{\what@do}[1]%
{\if\c@py\gobble@all@end%
 \gobble@one{#1}\else\gobble@all\fi}


\newenvironment{EXERCICE}{}{}
\newenvironment{SOLUTION}{}{}
%\excludecomment{EXERCICE}
%\excludecomment{SOLUTION}
\ifex@print%
%\includecomment{EXERCICE}
\else%
\renewenvironment{EXERCICE}{\gobble@true\relax\gobble@all}{\gobble@false\relax}
\fi
%\ifs@lprint%
%\includecomment{SOLUTION}{}{}
%\fi
\setlength{\parindent}{0pt}
\newcounter{exo}
\newcounter{solu}
\renewcommand\theexo{\@arabic\c@exo}
\renewcommand\thesolu{\@arabic\c@solu}

\newcounter{quesidepth}
\newcounter{quesi}[exo]
\newcounter{quesii}[quesi]
\newcounter{quesiii}[quesii]
\newlength{\topsepi}
\newlength{\topsepii}
\newlength{\topsepiii}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
%
\newcommand{\exercice}{%
\@startsection{exo}{0}{1ex}%
{-3.5ex \@plus -1ex \@minus -0.2ex}%
{2.3ex \@plus 0.2ex}%
{\normalfont\bfseries Exercice}%
}
%
\newcommand{\soluce}{%
\@startsection{solu}{0}{1ex}%
{-3.5ex \@plus -1ex \@minus -0.2ex}%
{2.3ex \@plus 0.2ex}%
{\normalfont\bfseries Solution de l'exercice}%
}
%
\newcommand{\labelquei}{\arabic{quesi}}
\newcommand{\labelqueii}{\labelquei.\alph{quesii}}
\newcommand{\labelqueiii}{\labelqueii.\roman{quesiii}}
\setlength{\topsepi}{1em plus 1em minus 0.3em}
\setlength{\topsepii}{0.25em plus 0.25em minus 0.075em}
\setlength{\topsepiii}{0.25em plus 0.25em minus 0.075em}
\newenvironment{questions}
{%
\stepcounter{quesidepth}
\begin{list}
{\ifnum\thequesidepth = 1
   \stepcounter{quesi}\labelquei
 \else
    \ifnum\thequesidepth = 2
      \stepcounter{quesii}\labelqueii
    \else
       \ifnum\thequesidepth = 3
          \stepcounter{quesiii}\labelqueiii
       \fi
    \fi
 \fi
}
{\ifnum\thequesidepth = 1
   \setlength{\topsep}{\topsepi}
 \else
    \ifnum\thequesidepth = 2
       \setlength{\topsep}{\topsepii}
    \else
       \ifnum\thequesidepth = 3
          \setlength{\topsep}{\topsepiii}
       \fi
    \fi
 \fi
}
}
{\end{list}
\addtocounter{quesidepth}{-1}
}
%
\newenvironment{donnees}[1][Données]
{%
\noindent\par\underline{#1~:}\nopagebreak[4]
\begin{list}
  {$\cdot$}
  {}
}
{\end{list}}
\@ifundefined{rappel}{%
\newlength{\raplength}
\settowidth{\raplength}{Rappel~:\ }
\newcommand{\rappel}[1]{%
\par\noindent%
Rappel~:\ \parbox[t]{\linewidth-\raplength}{\emph{#1}}
}
}{}
\newcounter{answer}[solu]
\newcounter{answeri}[answer]
\newcounter{answerii}[answeri]
\renewcommand{\theanswer}{\@arabic\c@answer}
\renewcommand{\theansweri}{\theanswer.\@alph\c@answeri}
\renewcommand{\theanswerii}{\theansweri.\@roman\c@answerii}
\newcommand{\reponse}{%
\@startsection{answer}{1}{\z@}%
{-3.25ex \@plus -1ex \@minus -0.2ex}%
{1.5ex \@plus 0.2ex}%
{\normalfont}%
}

\newcommand{\reponsea}{%
\@startsection{answeri}{2}{\z@}%
{-3.25ex \@plus -1ex \@minus -0.2ex}%
{1.5ex \@plus 0.2ex}%
{\normalfont}%
}

\newcommand{\reponseai}{%
\@startsection{answerii}{3}{\z@}%
{-3.25ex \@plus 1ex \@minus -0.2ex}%
{1em}%
{\normalfont}%
}

\AtBeginDocument{%
\setcounter{tocdepth}{4}
\setcounter{quesidepth}{0}
\setcounter{answer}{0}
\setcounter{quesii}{0}
\setcounter{quesiii}{0}
\setcounter{answeri}{0}
\setcounter{answerii}{0}
}
%% equation 
\@addtoreset{equation}{exo}
%% general defs
\@ifundefined{dd}{\newcommand{\dd}{\ensuremath{\mathrm{d}}}}{}
\@ifundefined{doverd}{\newcommand{\doverd}[2]{\ensuremath{\frac{\dd #1}{\dd #2}}}}{}
\@ifundefined{doverdt}{\newcommand{\doverdt}[1]{\doverd{#1}{t}}}{}
%%
\endinput
